package com.qf.fronted.service.impl;

import com.qf.fronted.domain.Goods;
import com.qf.fronted.mapper.GoodMapper;
import com.qf.fronted.service.ICartService;
import com.qf.fronted.service.IGoodService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodServiceImpl implements IGoodService {

    // 图片服务器的地址
    private final String imageServerLocation = "http://localhost/images/";

    private GoodMapper goodMapper;
    private ICartService cartServiceImpl;

    public GoodServiceImpl(GoodMapper goodMapper, ICartService cartServiceImpl) {
        this.goodMapper = goodMapper;
        this.cartServiceImpl = cartServiceImpl;
    }

    @Override
    public List<Goods> getAll() {
        List<Goods> list = goodMapper.getAll();
        // 是将图片设置为可直接访问的地址
        list.forEach(g -> {
            String imageSrc = g.getImageSrc(); // mobile/huawei.jpg
            g.setImageSrc(imageServerLocation + imageSrc);
        });
        return list;
    }

    @Override
    public List<Goods> getGoodsByIds(Integer[] ids) {
        List<Integer> lists=new ArrayList<>();
            for (Integer id : ids) {
                lists.add(id);
            }
        List<Goods> list = goodMapper.getGoodsByIds(lists);
        // 是将图片设置为可直接访问的地址
        list.forEach(g -> {
            String imageSrc = g.getImageSrc(); // mobile/huawei.jpg
            g.setImageSrc(imageServerLocation + imageSrc);
        });
        return list;
    }
}
